const jwt = require('jsonwebtoken');
const { getSecret } = require('../utils/index');

const config = getSecret();
const key = config.jwtSecret;

module.exports = {
  secretKey: key,
  authToken(req, res, next) {
    const token = req.get('token');
    if (!token) {
      return res.status(401).json({
        message: 'token 缺失!',
      });
    }
    jwt.verify(token, key, (err, data) => {
      if (err) {
        return res.status(401).json({ code: 401, message: 'token 过期' });
      }
      next();
    });
  },
};
